import 'package:acgn_pioneer/models/comment_model.dart';
import 'package:acgn_pioneer/widgets/reader/mixins/widget_base.dart';
import 'package:acgn_pioneer/widgets/reader/services/_index.dart';
import 'package:flutter/widgets.dart';
import '../../../public.dart';

/// 评论输入框
class ReaderCommentInput extends StatefulWidget {
  final CommentModel commentModel;
  final ChapterModel chapterModel;
  final String placeholder;
  // 是否是漫画主体评论
  final isComicType;

  ReaderCommentInput(
      {Key key,
      this.commentModel,
      this.chapterModel,
      this.placeholder,
      this.isComicType = false})
      : super(key: key);

  @override
  ReaderCommentInputState createState() => ReaderCommentInputState();
}

class ReaderCommentInputState extends State<ReaderCommentInput>
    with ReaderWidgetBase {
  final TextEditingController textEditingController = TextEditingController();
  final FocusNode focusNode = FocusNode();
  ReaderCommentService readerCommentService = ReaderCommentService();

  @override
  void initState() {
    super.initState();
    focusNode.requestFocus();
  }

  @override
  void dispose() {
    super.dispose();
    focusNode.dispose();
    textEditingController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: 750,
        padding: EdgeInsets.only(top: 10, right: 10, bottom: 10, left: 16),
        decoration: BoxDecoration(
            border: Border(top: BorderSide(color: Colors.black, width: 1))),
        child: Row(children: [
          Container(
              width: 320,
              margin: EdgeInsets.only(right: 6),
              padding: EdgeInsets.only(left: 16),
              decoration: BoxDecoration(
                  color: ColorUtil.rgba(40, 40, 56, 1),
                  borderRadius: BorderRadius.all(Radius.circular(16))),
              child: TextField(
                  minLines: 1,
                  maxLines: 4,
                  maxLength: 100,
                  focusNode: focusNode,
                  controller: textEditingController,
                  cursorColor: Colors.red,
                  onSubmitted: (String value) {},
                  onChanged: (String value) {},
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                  ),
                  decoration: InputDecoration(
                    isDense: true,
                    counterText: '',
                    focusColor: ColorUtil.rgba(255, 255, 255, 0.7),
                    hintText: widget.placeholder,
                    hintStyle:
                        TextStyle(color: ColorUtil.rgba(255, 255, 255, 0.3)),
                    errorBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.transparent)),
                    focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.transparent)),
                  ))),
          buildSend()
        ]));
  }

  buildSend() {
    return GestureDetector(
      child: Text('发送', style: TextStyle(color: Colors.white)),
      onTap: () {
        var comic = widget.chapterModel.comicModel;
        readerCommentService.addComment(CommentModel(
            content: textEditingController.text,
            title: comic.name,
            relateId: widget.isComicType ? comic.id : widget.chapterModel.id,
            fatherId: widget.commentModel == null ? 0 : widget.commentModel.id,
            ssid: comic.id));
      },
    );
  }
}
